home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dviapollo / pxl2apollo.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  10KB  |  331 lines

  1. /*    PXL2APOLLO -- TeX PXL Font to Augmented Apollo Font Converter         */
  2. /*                                         */
  3. /*    Copyright (C) 1987 by Leonard N. Zubkoff, All Rights Reserved         */
  4. /*                                         */
  5. /*    This software is provided free and without any warranty.         */
  6. /*    Permission to copy for any purpose is hereby granted so             */
  7. /*    long as this copyright notice remains intact.                 */
  8. /*                                         */
  9. /*    Revision:     1-Jun-88 09:41:37                     */
  10.  
  11.  
  12. #define begin        {
  13. #define end        }
  14. #define then
  15. #define do
  16. #define hidden        static
  17. #define visible
  18. #define procedure   void
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <strings.h>
  23.  
  24.  
  25. #include "/sys/ins/base.ins.c"
  26. #include "/sys/ins/gpr.ins.c"
  27. #include "/sys/ins/ios.ins.c"
  28. #include "/sys/ins/ms.ins.c"
  29. #include "/sys/ins/smdu.ins.c"
  30.  
  31.  
  32. #define FontBitmapWidth        224
  33.  
  34.  
  35. hidden char
  36.     *PXLFileName,
  37.     *FontFilePointer;
  38.  
  39.  
  40. hidden long
  41.     FontFileLength;
  42.  
  43. struct PXL_Header
  44.     begin
  45.     long PXLID1;
  46.     end;
  47.     
  48.  
  49. struct PXL_Trailer
  50.     begin
  51.     long CheckSum;
  52.     long Magnification;
  53.     long DesignSize;
  54.     long DirectoryPointer;
  55.     long PXLID2;
  56.     end;
  57.  
  58.  
  59. struct PXL_CharacterEntry
  60.     begin
  61.     short PixelWidth;
  62.     short PixelHeight;
  63.     short Xoffset;
  64.     short Yoffset;
  65.     long RasterAddress;
  66.     long TFMwidth;
  67.     end;
  68.  
  69.  
  70. struct AAF_Trailer
  71.     begin
  72.     long TFMwidth[128];
  73.     struct PXL_Trailer PXL_TrailerCopy;
  74.     end;
  75.  
  76.  
  77. hidden struct PXL_Header
  78.     PXL_FileHeader;
  79.  
  80.  
  81. hidden struct PXL_Trailer
  82.     PXL_FileTrailer;
  83.  
  84.  
  85. hidden struct PXL_CharacterEntry
  86.     PXL_FontDirectory[128];
  87.  
  88.  
  89. hidden gpr_$bitmap_desc_t
  90.     FontBitmap,
  91.     CharacterBitmaps[128];
  92.  
  93.  
  94. hidden gpr_$attribute_desc_t
  95.     FontAttributeBlock;
  96.  
  97. hidden procedure FatalError(Message)
  98.     char *Message;
  99.     begin
  100.     if (*PXLFileName != '\0') then
  101.         fprintf(stderr,"pxl2apollo: %s - %s\n",PXLFileName,Message);
  102.     else fprintf(stderr,"pxl2apollo: %s\n",Message);
  103.     exit(1);
  104.     end
  105.  
  106.  
  107. hidden procedure LoadRasters(PXLStream)
  108.     begin
  109.     int Character;
  110.     for (Character=0; Character<128; Character++) do
  111.         if (PXL_FontDirectory[Character].RasterAddress != 0) then
  112.         begin
  113.             gpr_$offset_t CharacterBitmapSize;
  114.             status_$t Status;
  115.             static short Buffer[64];
  116.             short *CharacterBitmapPointer, CharacterLineWidth;
  117.             int Words, Halfwords, Row, i;
  118.             fseek(PXLStream,
  119.               4*PXL_FontDirectory[Character].RasterAddress,0);
  120.             if (PXL_FontDirectory[Character].PixelWidth == 0
  121.             || PXL_FontDirectory[Character].PixelHeight == 0) then
  122.                 continue;
  123.             CharacterBitmapSize.x_size =
  124.             PXL_FontDirectory[Character].PixelWidth;
  125.             CharacterBitmapSize.y_size =
  126.             PXL_FontDirectory[Character].PixelHeight;
  127.             gpr_$allocate_bitmap(CharacterBitmapSize,0,
  128.                      FontAttributeBlock,
  129.                      CharacterBitmaps[Character],Status);
  130.             if (Status.all != status_$ok) then pfm_$error_trap(Status);
  131.             gpr_$inq_bitmap_pointer(CharacterBitmaps[Character],
  132.                         CharacterBitmapPointer,
  133.                         CharacterLineWidth,Status);
  134.             if (Status.all != status_$ok) then pfm_$error_trap(Status);
  135.             Halfwords = 1+((CharacterBitmapSize.x_size-1)>>4);
  136.             Words = 1+((Halfwords-1)>>1);
  137.             for (Row=0; Row<CharacterBitmapSize.y_size; Row++) do
  138.             begin
  139.                 register short *Pointer = Buffer;
  140.                 fread(Buffer,4,Words,PXLStream);
  141.                 for (i=0; i<Halfwords; i++) do
  142.                 *CharacterBitmapPointer++ = *Pointer++;
  143.                 CharacterBitmapPointer += CharacterLineWidth-Halfwords;
  144.             end;
  145.         end;
  146.     end
  147.  
  148. hidden procedure CreateFont()
  149.     begin
  150.     smd_$font_table_t *FontTable = (smd_$font_table_t *) FontFilePointer;
  151.     gpr_$window_t SourceWindow;
  152.     gpr_$position_t DestinationOrigin;
  153.     status_$t Status;
  154.     int FontCharacterCount = 0, MaxHeight = 0, MaxWidth = 0;
  155.     int MaxRight = 0, MaxUp = 0, MaxDown = 0, MaxHeightRow = 0;
  156.     int Xposition = 0, Yposition = 0, Character;
  157.     short *FontBitmapPointer, *Pointer, FontLineWidth, Row, i;
  158.     struct AAF_Trailer *TrailerPointer;
  159.     for (Character=0; Character<128; Character++) do
  160.         if (PXL_FontDirectory[Character].RasterAddress != 0) then
  161.         begin
  162.             /* The rounding is emperically determined. */
  163.             int Width = (PXL_FontDirectory[Character].TFMwidth
  164.                  *(PXL_FileTrailer.Magnification/1000.0)
  165.                  *123.0/4736286.72);
  166.             int PixelWidth = PXL_FontDirectory[Character].PixelWidth;
  167.             int PixelHeight = PXL_FontDirectory[Character].PixelHeight;
  168.             int Xoffset = PXL_FontDirectory[Character].Xoffset;
  169.             int Yoffset = PXL_FontDirectory[Character].Yoffset;
  170.             int Left = Xoffset;
  171.             int Right = PixelWidth-Xoffset;
  172.             int Up = Yoffset;
  173.             int Down = PixelHeight-Yoffset;
  174.             FontCharacterCount++;
  175.             if (Xposition+PixelWidth > FontBitmapWidth) then
  176.             begin
  177.                 Xposition = 0;
  178.                 Yposition += MaxHeightRow;
  179.                 MaxHeightRow = 0;
  180.             end;
  181.             FontTable->index_table[Character] = FontCharacterCount;
  182.             FontTable->desc_table[FontCharacterCount-1].left = Left;
  183.             FontTable->desc_table[FontCharacterCount-1].right = Right;
  184.             FontTable->desc_table[FontCharacterCount-1].up = Up;
  185.             FontTable->desc_table[FontCharacterCount-1].down = Down;
  186.             FontTable->desc_table[FontCharacterCount-1].width = Width;
  187.             FontTable->desc_table[FontCharacterCount-1].x_pos = Xposition;
  188.             FontTable->desc_table[FontCharacterCount-1].y_pos = Yposition;
  189.             if (Right > MaxRight) then MaxRight = Right;
  190.             if (Up > MaxUp) then MaxUp = Up;
  191.             if (Down > MaxDown) then MaxDown = Down;
  192.             if (PixelHeight > MaxHeight) then MaxHeight = PixelHeight;
  193.             if (Width > MaxWidth) then MaxWidth = Width;
  194.             if (PixelHeight > MaxHeightRow) then MaxHeightRow = PixelHeight;
  195.             if (PixelWidth == 0 || PixelHeight == 0) then continue;
  196.             SourceWindow.window_base.x_coord = 0;
  197.             SourceWindow.window_base.y_coord = 0;
  198.             SourceWindow.window_size.x_size = PixelWidth;
  199.             SourceWindow.window_size.y_size = PixelHeight;
  200.             DestinationOrigin.x_coord = Xposition;
  201.             DestinationOrigin.y_coord = Yposition;
  202.             gpr_$pixel_blt(CharacterBitmaps[Character],SourceWindow,
  203.                    DestinationOrigin,Status);
  204.             if (Status.all != status_$ok) then pfm_$error_trap(Status);
  205.             Xposition += PixelWidth;
  206.         end
  207.  
  208.         else FontTable->index_table[Character] = 0;
  209.     FontTable->version = 1;
  210.     FontTable->image_offset = sizeof(smd_$font_table_t);
  211.     FontTable->chars_in_font = FontCharacterCount;
  212.     FontTable->raster_lines = Yposition+(Xposition > 0 ? MaxHeightRow : 0);
  213.     FontTable->image_size = FontTable->raster_lines*FontBitmapWidth/8;
  214.     FontTable->max_height = MaxHeight;
  215.     FontTable->max_width = MaxWidth;
  216.     FontTable->v_spacing = 3;
  217.     FontTable->h_spacing = 1;
  218.     FontTable->space_size = MaxWidth/3;
  219.     FontTable->max_right = MaxRight;
  220.     FontTable->max_up = MaxUp;
  221.     FontTable->max_down = MaxDown;
  222.     gpr_$inq_bitmap_pointer(FontBitmap,FontBitmapPointer,
  223.                 FontLineWidth,Status);
  224.     if (Status.all != status_$ok) then pfm_$error_trap(Status);
  225.     Pointer = (short *) (FontFilePointer+sizeof(smd_$font_table_t));
  226.     for (Row=0; Row<FontTable->raster_lines; Row++) do
  227.         begin
  228.         for (i=0; i<FontBitmapWidth/16; i++) do
  229.             *Pointer++ = *FontBitmapPointer++;
  230.         FontBitmapPointer += FontLineWidth-(FontBitmapWidth/16);
  231.         end;
  232.     FontFileLength = FontTable->image_offset+FontTable->image_size;
  233.     TrailerPointer =
  234.         (struct AAF_Trailer *) (((char *) FontFilePointer)+FontFileLength);
  235.     for (Character=0; Character<128; Character++) do
  236.         TrailerPointer->TFMwidth[Character] =
  237.         PXL_FontDirectory[Character].TFMwidth;
  238.     TrailerPointer->PXL_TrailerCopy = PXL_FileTrailer;
  239.     TrailerPointer->PXL_TrailerCopy.DirectoryPointer = 0;
  240.     FontFileLength += sizeof(struct AAF_Trailer);
  241.     end
  242.  
  243.  
  244. hidden procedure ProcessFile(PXLStream)
  245.     FILE *PXLStream;
  246.     begin
  247.     static gpr_$offset_t FontBitmapSize = { FontBitmapWidth, gpr_$max_y_size };
  248.     status_$t Status;
  249.     gpr_$init(gpr_$no_display,0,FontBitmapSize,0,FontBitmap,Status);
  250.     if (Status.all != status_$ok) then pfm_$error_trap(Status);
  251.     FontAttributeBlock = gpr_$attribute_block(FontBitmap,Status);
  252.     if (Status.all != status_$ok) then pfm_$error_trap(Status);
  253.     fread(&PXL_FileHeader,sizeof(PXL_FileHeader),1,PXLStream);
  254.     if (PXL_FileHeader.PXLID1 != 1001) then
  255.         FatalError("invalid pxl file");
  256.     fseek(PXLStream,-sizeof(PXL_FontDirectory)-sizeof(PXL_FileTrailer),2);
  257.     fread(PXL_FontDirectory,sizeof(PXL_FontDirectory),1,PXLStream);
  258.     fread(&PXL_FileTrailer,sizeof(PXL_FileTrailer),1,PXLStream);
  259.     if (PXL_FileTrailer.PXLID2 != 1001) then
  260.         FatalError("invalid pxl file");
  261.     LoadRasters(PXLStream);
  262.     CreateFont();
  263.     gpr_$terminate(true,Status);
  264.     end
  265.  
  266. visible int main(ArgCount,ArgVector,Environment)
  267.     int ArgCount;
  268.     char *ArgVector[], *Environment[];
  269.     begin
  270.     FILE *PXLStream;
  271.     char FontFileName[256], *LastSlash;
  272.     ios_$id_t StreamID;
  273.     extern uid_$t unstruct_$uid;
  274.     status_$t Status;
  275.     int CurrentArg;
  276.     if (ArgCount < 2) then
  277.         begin
  278.         fprintf(stderr,"Usage: pxl2apollo {filename}*\n");
  279.         exit(1);
  280.         end;
  281.     for (CurrentArg=1; CurrentArg<ArgCount; CurrentArg++) do
  282.         begin
  283.         PXLFileName = ArgVector[CurrentArg];
  284.         if ((LastSlash = rindex(PXLFileName,'/')) != NULL) then
  285.             begin
  286.             LastSlash++;
  287.             strcpy(FontFileName,LastSlash);
  288.             end
  289.         else strcpy(FontFileName,PXLFileName);
  290.         FontFileName[strlen(FontFileName)-3] = 'a';
  291.         FontFileName[strlen(FontFileName)-2] = 'a';
  292.         FontFileName[strlen(FontFileName)-1] = 'f';
  293.         printf("pxl2apollo: %s ==> %s\n",PXLFileName,FontFileName);
  294.         if ((PXLStream = fopen(PXLFileName,"r")) == NULL) then
  295.             begin
  296.             fprintf(stderr,"pxl2apollo: %s - cannot open for input\n",
  297.                 PXLFileName);
  298.             continue;
  299.             end;
  300.         ios_$create(*FontFileName,(short)strlen(FontFileName),
  301.                 unstruct_$uid,ios_$recreate_mode,ios_$write_opt,
  302.                 StreamID,Status);
  303.         if (Status.all != status_$ok) then
  304.             begin
  305.             fprintf(stderr,"pxl2apollo: %s - cannot create file\n",
  306.                 FontFileName);
  307.             fclose(PXLStream);
  308.             continue;
  309.             end;
  310.         ios_$close(StreamID,Status);
  311.         FontFilePointer =
  312.             ms_$mapl(*FontFileName,(short)strlen(FontFileName),
  313.                  0L,0x40000L,ms_$nr_xor_1w,ms_$wr,ms_$extend,
  314.                  FontFileLength,Status);
  315.         if (Status.all != status_$ok) then
  316.             begin
  317.             fprintf(stderr,"pxl2apollo: %s - cannot map file\n",
  318.                 FontFileName);
  319.             fclose(PXLStream);
  320.             continue;
  321.             end;
  322.         ProcessFile(PXLStream);
  323.         fclose(PXLStream);
  324.         ms_$truncate(FontFilePointer,FontFileLength,Status);
  325.         if (Status.all != status_$ok) then pfm_$error_trap(Status);
  326.         ms_$unmap(FontFilePointer,0x40000L,Status);
  327.         if (Status.all != status_$ok) then pfm_$error_trap(Status);
  328.         end;
  329.     return 0;
  330.     end
  331.